@extends('admin.master')

@section('page_header')
    <h1 class="page-title">
        <i class="voyager-person "></i>用户列表
        @if ($add_user)
            <a href="{{ route('users.add') }}" class="btn btn-success" target="_blank">
                <i class="voyager-plus"></i> 添加用户
            </a>
        @endif
    </h1>
@stop

@section('page_header_actions')

@stop

@section('content')
    <div class="page-content container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-bordered">
                    <div class="panel-body">
                        <table id="dataTable" class="table table-hover">
                            <thead>
                            <tr>
                                <th>姓名</th>
                                <th>手机号</th>
                                <th>邮箱</th>
                                <th>角色</th>
                                <th>所属地区</th>
                                <th class="actions" style="width:200px;">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($users as $data)
                                <tr>
                                    <td>{{$data->name}}</td>
                                    <td>{{$data->mobile}}</td>
                                    <td>{{$data->email}}</td>
                                    <td>{{$data->role->name}}</td>
                                    <td>{{$data->address}}</td>
                                    <td class="no-sort no-click">
                                        @if ($del_user)
                                            <div class="btn-sm btn-danger pull-right delete" data-id="{{ $data->id }}"
                                                 id="delete-{{ $data->id }}">
                                                <i class="voyager-trash"></i> 删除
                                            </div>
                                        @endif
                                        @if ($edit_user)
                                            <a href="{{ route('users.add', $data->id) }}" target="_blank"
                                               class="btn-sm btn-primary pull-right edit">
                                                <i class="voyager-edit"></i> 编辑
                                            </a>
                                        @endif
                                        <a href="{{ route('profile.index', $data->id) }}" target="_blank"
                                           class="btn-sm btn-warning pull-right">
                                            <i class="voyager-eye"></i> 查看
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal modal-danger fade" tabindex="-1" id="confirm_modal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"><i class="voyager-trash"></i>确认删除该用户?</h4>
                </div>
                <div class="modal-footer">
                    <button id="confirm-btn" class="btn btn-danger pull-right">确认</button>
                    <button type="button" class="btn btn-default pull-right" data-dismiss="modal">取消</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <div class="modal modal-danger fade" tabindex="-1" id="delete_modal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">请选择另外一个用户接手该用户的客源，房源</h4>
                </div>
                <div class="modal-footer">
                    <form action="{{ route('users.delete') }}" id="delete_form" method="POST">
                        {{ method_field("DELETE") }}
                        {{ csrf_field() }}
                        <select name="newUser" id="newUser" class="form-control" style="margin-bottom: 50px;">
                            <option value="null">--请选择员工--</option>
                        </select>

                        <input type="submit" class="btn btn-danger pull-right delete-confirm"
                               value="确认">
                    </form>
                    <button type="button" class="btn btn-default pull-right" data-dismiss="modal">取消</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
@stop
@section('javascript')
    <!-- DataTables -->
    <script>
        $(document).ready(function () {
            $('#dataTable').DataTable({"order": []});
        });
        $('td').on('click', '.delete', function (e) {
            var form = $('#delete_form')[0];
            form.action = parseActionUrl(form.action, $(this).data('id'));
            $("#newUser option").each(function () {
                if ($(this).val() != 'null') {
                    $(this).remove();
                }
            });
            var str = '';
            $.ajax({
                url: "{{ route('users.choose_new_user') }}",
                data: {
                    'id': $(this).data('id'),
                    'token': '{{ csrf_field() }}'
                },
                type: 'GET',
                dataType: 'JSON',
                async: false,
                success: function (datas) {
                    if (datas.length != 0) {
                        datas.forEach(function (data) {
                            str += '<option value=' + data.id + '>' + data.name + '</option>';
                        })
                    }
                    $('#newUser').append(str);
                }
            });
            $('#confirm_modal').modal('show');
        });
        function parseActionUrl(action, id) {
            return action.match(/\/[0-9]+$/)
                ? action.replace(/([0-9]+$)/, id)
                : action + '/' + id;
        }
        $("#confirm-btn").click(function () {
            $('#confirm_modal').modal('hide');
            $('#delete_modal').modal('show');
        });
    </script>
@stop
